package com.sz.common.pay.common;


import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by siqishangshu on 2017-10-30.
 */
public class PayProperties {
    public static final String SPLIT = ",";
    public static final String FILE_NAME = "payment.properties";
    private static Map instance = new HashMap();

    static {
        refresh();
    }

    public static Map getProperties() {
        return instance;
    }


    public static void refresh() {
        Properties prop = new Properties();
        InputStream in = null;
        try {
            in = PayProperties.class.getClassLoader().getResourceAsStream(FILE_NAME);
            prop.load(in);
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                instance.put(key, prop.getProperty(key));
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static List getOrDefault(String key, List defaultValue) {
            if (instance.containsKey(key)) {
                List list = new ArrayList();
                String value = instance.get(key).toString();
                String[] arr = value.split(SPLIT);
                for (String s : arr) {
                    list.add(s);
                }
                return list;
            }
            return defaultValue;
    }

    public static String getOrDefault(String key, String defaultValue) {
            if (instance.containsKey(key)) {
                return instance.get(key).toString();
            }
            return defaultValue;
    }

    public static int getOrDefault(String key, int defaultValue) {
            if (instance.containsKey(key)) {
                return Integer.parseInt(instance.get(key).toString());
            }
            return defaultValue;
    }

    public static double getOrDefault(String key, double defaultValue) {
            if (instance.containsKey(key)) {
                return Double.parseDouble(instance.get(key).toString());
            }
            return defaultValue;
    }
}
